﻿using EzDesk.DesktopClient.Model;

namespace EzDesk.DesktopClient.ViewModel
{
    using System.Collections.ObjectModel;
    using System.Windows.Threading;
    using System.Threading;

    using EzDesk.MVVMBase;
    using EzDesk.EventAggregation;
    using EzDesk.DesktopClient.Event;
    using EzDesk.Generic;
    using EzDesk.Generic.Collections;
    using EzDesk.DesktopClient.ViewContract;
    using EzDesk.Client.Utility;
    using System;
    using EzDesk.DesktopClient.View;
    using EzDesk.DesktopClient.MVVMEnhance;

    public class NotificationViewModel : ViewModelWithSingleContainer<INotificationView>
    {

        #region Fields
        private int _selectedIndex = 0;
        private int _preloadCount = 30;
        private RangeCollection<FeedItemViewModel> _tipPopFeeds;

        //Should read from config
        private bool _autoShow
        {
            get
            {
                return true;
            }
        }

        #endregion Fields

        #region Constructor
        public NotificationViewModel()
            : base()
        {
            InitField();
            InitEvent();
        }

        #region Init
        private void InitField()
        {
            _tipPopFeeds = new RangeCollection<FeedItemViewModel>(_preloadCount);
        }
        private void InitEvent()
        {
            InitGlobalEvent();
            InitLocalEvent();
        }
        private void InitGlobalEvent()
        {
            FeedUpdateEvent.Instance.Subscribe(this.OnFeedUpdate,ThreadOption.UIThread);
            
        }
        private void InitLocalEvent()
        {
            View.OnGoNext += (obj, e) =>
                {
                    SelectedIndex++;
                };

            View.OnGoPrev += (obj, e) =>
                {
                    SelectedIndex--;
                };
        }
        #endregion Init

        #endregion Constructor

        #region Properties

        public int SelectedIndex
        {
            get
            {
                return _selectedIndex + 1;
            }
            set
            {
                if (value >= 1 && value <= _tipPopFeeds.Count)
                {
                    _selectedIndex = value - 1;
                    this.RaisePropertyChanged("SelectedIndex");
                    this.RaisePropertyChanged("CurrentFeedViewModel");
                }
            }
        }
        public FeedItemViewModel CurrentFeedViewModel
        {
            get
            {
                if (_tipPopFeeds.Count == 0) return null;
                return _tipPopFeeds[_selectedIndex];
            }
        }
        #endregion Properties

        #region EventMethod
        public void OnFeedUpdate(FeedUpdateArgs args)
        {
            ProcessView();
            ProcessData(args);
        }
        private void ProcessData(FeedUpdateArgs args)
        {
            Action<Feed> generateView = (model) =>
            {
                FeedItemViewModel viewModel = new FeedItemViewModel(model);
                _tipPopFeeds.Insert(0, viewModel);
            };

            args.UpdateCollection.Foreach(generateView);
            args.NotificationCollection.Foreach(generateView);
            
            SelectedIndex = 1;
        }
        private void ProcessView()
        {
            _autoShow.TrueDo(View.NotificationShow);
        }
        #endregion EventMethod
    }
}